class Solution(object):
    def findPoisonedDuration(self, timeSeries, duration):
        """
        :type timeSeries: List[int]
        :type duration: int
        :rtype: int
        """
        total_time = 0
        for i in range(len(timeSeries)):
            if i == len(timeSeries)-1 or timeSeries[i] + duration - 1 < timeSeries[i+1]:
                total_time += duration
            else:
                total_time += timeSeries[i+1] - timeSeries[i]
        return total_time
if __name__ == '__main__':
    timeSeries = [1,2]
    duration = 2
    print(Solution().findPoisonedDuration(timeSeries, duration))
# 思路：
# 遍历数组，如果当前元素和下一个元素的差小于duration，则加上duration，否则加上差值。
# 最后返回总时间。
# 时间复杂度：O(n)
# 空间复杂度：O(1)